using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Animoso.Domain;
using Animoso.Domain.Data;

namespace Animoso.Web.Controllers
{
    public class ManageAdministrationController : ApplicationController
    {
        private IRepository<Commission> _commissionRepository;
        private IRepository<Administration> _administrationRepository;

        public ManageAdministrationController(IRepository<Commission> commissionRepository, IRepository<Administration> administrationRepository)
        {
            _commissionRepository = commissionRepository;
            _administrationRepository = administrationRepository;
        }

        public ActionResult Index()
        {
            ViewData["commissions"] = _commissionRepository.GetAll();
            
            return View(_administrationRepository.GetAll());
        }

        public string GetCommissionName(int commissionId)
        {
            return _commissionRepository.GetById(commissionId).Name;
        }

        private void SetCommissionListItem()
        {
            IEnumerable<Commission> commissions = _commissionRepository.GetAll();
            IList<SelectListItem> commissionsListItems =
                commissions.Select(commission => new SelectListItem() {Text = commission.Name, Value = commission.Id.ToString()}).
                    ToList();

            ViewData["CommissionSelect"] = commissionsListItems;
        }

        private void SetRoleListItem()
        {
            IEnumerable<Role> roles = Enum.GetValues(typeof(Role)).Cast<Role>();
            IList<SelectListItem> roleListItems = roles.Select(role => new SelectListItem() { Text = role.ToString(), Value = role.ToString() }).ToList();

            ViewData["RoleSelect"] = roleListItems;
        }

        public ActionResult Create()
        {
            SetRoleListItem();
            ViewData["Commissions"] = _commissionRepository.GetAll();
            Administration administration = new Administration();
            administration.StartDate = DateTime.Now;
            _administrationRepository.Add(administration);
            return View("Edit", administration);
        }

        public JsonResult SetCommissionName(int commissionId, string name)
        {
            Commission commission = _commissionRepository.GetById(commissionId);
            commission.Name = name;
            _commissionRepository.Save(commission);

            return Json(commission);
        }

        public JsonResult DeleteCommission(int commissionId)
        {
            Commission commission = _commissionRepository.GetById(commissionId);
            if (commission == null)
                return Json(false);
            _commissionRepository.Delete(commission);
            return Json(true);
        }

        public JsonResult AddCommission(string name)
        {
            Commission commission = new Commission() {Name = name};
            _commissionRepository.Add(commission);
            return Json(commission);
        }

        public JsonResult AddMember(int administrationId, string firstName, string lastName, Role role, int commissionId)
        {
            Administration administration = _administrationRepository.GetById(administrationId);

            Member member = new Member();
            member.FirstName = firstName;
            member.LastName = lastName;
            if (role == Role.Chairman)
            {
                Commission commission = _commissionRepository.GetById(commissionId);
                member.Commission = commission;
            }
            member.Role = role;
            

            administration.AddMember(member);
            _administrationRepository.Save(administration);

            return Json(member);
        }

        private string MapPath(string fileName)
        {
            return Server.MapPath("\\" + Folders.ADMINISTRATION_PICTURE_FOLDER) + "\\" + fileName;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string Upload(HttpPostedFileBase FileData, int administrationId)
        {
            Administration administration = _administrationRepository.GetById(administrationId);

            if (!string.IsNullOrEmpty(administration.Picture))
            {
                System.IO.File.Delete(MapPath(administration.Picture));
            }

            Guid pictureGuid = Guid.NewGuid();
            string fileName = pictureGuid.ToString() + "." +
                               FileData.FileName.Split('.')[1];

            string pathToImg = MapPath(fileName);

            FileData.SaveAs(pathToImg);

            administration.Picture = fileName;
            _administrationRepository.Save(administration);

            return fileName;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Administration administration)
        {
            if (!ModelState.IsValid)
            {
                ViewData["Commissions"] = _commissionRepository.GetAll();
                return View("Edit", administration);
            }

            Administration administrationDb = _administrationRepository.GetById(administration.Id);
            administrationDb.EndDate = administration.EndDate;
            administrationDb.StartDate = administration.StartDate;

            _administrationRepository.Save(administrationDb);

            return RedirectToAction("Index", "ManageAdministration");
        }
    }
}
